package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IAuthority;
import service.interf.security.IGroup;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.Group;

public class GroupAction extends MyBaseAction<Group> implements
		ModelDriven<Group> {
	private static final long serialVersionUID = 2365964690869064854L;

	private IAuthority iAuth;
	private IGroup iGroup;
	private String groupAu;
	
	/************************* setter and getter *******************************/
	public IAuthority getiAuth() {
		return iAuth;
	}
	
	public void setiAuth(IAuthority iAuth) {
		this.iAuth = iAuth;
	}

	
	public String getGroupAu() {
		return groupAu;
	}


	public void setGroupAu(String groupAu) {
		this.groupAu = groupAu;
	}

	public void setiGroup(IGroup iGroup) {
		this.iGroup = iGroup;
	}
	
	public IGroup getiGroup() {
		return iGroup;
	}
	
	public Group getModel() {
		return model;
	}

	// constructor
	public GroupAction() {
		super(Group.class);
		validRules.clear();
		validRules.put("groupId", "empty");
		validRules.put("groupName", "empty|maxLength(50)");
		validRules.put("userName", "empty|betweenLength(3,30)");
		validRules.put("groups", "empty");
		validRules.put("users", "empty");
		validRules.put("groupAuthority", "empty");
		validRules.put("datas", "empty");
		validRules.put("ids","empty");
	}

	/**************************** method ***************************************/

	/**
	 * 添加组
	 */
	public void add() {
		if (!validMe("groupName"))
			return;
		String x = "1";
		try {
			iAuth.addRecord(model);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 删除组
	 */
	public void del() {
		if (!validMe("groupId"))
			return;
		String x = "1";
		try {
			iAuth.delRecord("Group",model.getGroupId());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}
	public void delBatch(){
		if(!validMe("ids")){
			return;
		}
		String x = "1";
		try{
			iAuth.delByIds("Group","groupId",model.getIds());
		}catch(Exception e){
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(),x);
	}
	/**
	 * 批量保存
	 */
	public void batch() {
		if (!validMe("ids"))
			return;
		String x = "1";
		try {
			iAuth.batchSave("Group", model.getDatas(),null);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 更新组权限
	 */
	public void updateGroupAuths() {
		if (!validMe("groupId,groupAu"))
			return;
		String x;
		try {
			x = iAuth.updateGroupAuthority(model.getGroupId(), groupAu );
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 通过组获取权限
	 */
	public void getAuthsByGroup() {
		if (!validMe("groupId"))
			return;
		String x;
		try {
			x = iAuth.findByProperty("GroupAuthority", "*,authority.authority(authority)", "json", true, 
					new String[]{"group.groupId"}, new Object[]{model.getGroupId()}, 
					null, null, model.getPage(), model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 获取所有组
	 */
	public void getAllGroups() {
		String x=null;
		try {
			x = iAuth.findByProperty("Group", "*", "json", true, null,
					null, null, null, model.getPage(), model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 获取所有的组
	 */
	public void getAll(){
		String r = "";
		try{
			r = iGroup.findByProperty(
					"Group", 
					"*", 
					"json", 
					true, 
					null, 
					null, 
					null, 
					null, 
					null, 
					null);
		}catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
	/*
	 * 通过Id获取组信息
	 */
	public void getGroupById(){
		String s = null;
		try{
			s = iGroup.findByProperty("Group", "*", 
					"json",
					true, 
					new String[]{"groupId"}, 
					new Object[]{model.getGroupId()},
					null, 
					null, 
					model.getPage(), 
					model.getRows());
		}catch(Exception e){
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(),s);
	}
	
	/**
	 * 获取所有组（带权限）
	 */
	public void getAllGroupsWithAuth() {
		String x;
		try {
			x = iAuth.getAllGroupsWithAuthority();
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}
	/**
	 * 修改组
	 */
	public void edit(){
		String s = "1";
		try{
			iGroup.editRecord(model);
		}catch(Exception e){
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(),s);
	}

	/**
	 * 添加多个用户到组
	 *//*
	public void addUsersToGroup() {
		if (!validMe("users,groupId"))
			return;
		String x;
		try {
			x = iAuth.addUsersToGroup(model.getUsers(), model.getGroupId());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	*//**
	 * 添加用户到用户组（通过用户组名）
	 *//*
	public void addUserToGroupByName() {
		if (!validMe("userId,groupName"))
			return;
		String r;
		try {
			r = iAuth.addUserToGroup(model.getUserId(), model.getGroupName());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	*//**
	 * 从组删除多个用户
	 *//*
	public void removeUsersFromGroup() {
		if (!validMe("userId,groupId"))
			return;
		String x;
		try {
			x = iAuth
					.removeUserFromGroup(model.getUserId(), model.getGroupId());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	*//**
	 * 从组移除用户（组名）
	 *//*
	public void removeUserFromGroupByName() {
		if (!validMe("userId,groupName"))
			return;
		String x;
		try {
			x = iAuth.removeUserFromGroup(model.getUserId(), model
					.getGroupName());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	*//**
	 * 删除用户组
	 *//*
	
	 * public void removeUserGroup(){ if (!validMe("userName")) return; String
	 * x; try{ x = iAuth.removeUserGroups(model.getUserId()); }catch(Exception
	 * e){ x = errorHandle.handleErr(e); }
	 * this.servletHandle.writeToClient(ServletActionContext.getResponse(),x); }
	 

	*//**
	 * 更新用户组
	 *//*
	public void updateUserGroup() {
		if (!validMe("userName"))
			return;
		String x;
		try {
			x = iAuth.updateUserGroups(model.getUserName(), model.getGroups());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	*//**
	 * 获取用户所在组
	 *//*
	
	  public void getUserGroups(){ 
		  if (!validMe("userName")) 
			  return; 
		  String x;
		  try{
			  x = iAuth.getUserGroups(model.getUserId());
		  }catch(Exception e){
			  x = errorHandle.handleErr(e); 
		  }
		  this.servletHandle.writeToClient(ServletActionContext.getResponse(),x);
		 }
	 */
	/**
	 * 获取组用户
	 *//*
	public void getGroupUsers() {
		if (!validMe("groupId"))
			return;
		String x;
		try {
			x = iAuth.getGroupUsers(model.getGroupId());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	*//**
	 * 获取组用户（带细节）
	 */
	public void getGroupUsersDetail() {
		if (!validMe("groupId"))
			return;
		String x;
		try {
			x = iAuth.getGroupUsersDetail(model.getGroupId(), model.getPage(), model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}
	/**
	 * 获取用户组
	 *//*
	public void getUserGroupsbyName() {
		if (!validMe("userName"))
			return;
		String x;
		try {
			x = iAuth.getUserGroups(model.getUserName());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	public void getUserGroupsbyId() {
		if (!validMe("groupId"))
			return;
		String x;
		try {
			x = iAuth.getUserGroups(Long.parseLong(model.getGroupId()
					.toString()));
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}
*/
}
